package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.entity.GarageStall;
import cn.mafangui.hotel.req.AddStallReq;
import cn.mafangui.hotel.enums.ExceptionEnum;
import cn.mafangui.hotel.exception.HotelException;
import cn.mafangui.hotel.req.PageQueryReq;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.GarageStallService;
import cn.mafangui.hotel.utils.AssertUtils;
import cn.mafangui.hotel.utils.PropInfoConstant;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: kalpa
 * @date: 2022/3/18
 * @version: 1.0
 * Description: No Description
 */
@Api( tags = {"操作员-车位管理"})
@RestController
@RequestMapping(value = "/op/stall")
public class OpStallController {
    @Autowired
    private GarageStallService garageStallService;

    @ApiOperation("增加车位信息")
    @PostMapping("/add")
    public AjaxResult add(AddStallReq addStallReq) throws HotelException {
        int rel = garageStallService.add(addStallReq);
        AssertUtils.assertTrue(rel > 0, ExceptionEnum.STALL_ADD_FAIED);

        return ResponseTool.success(PropInfoConstant.ADD_SUCCESS);
    }

    @ApiOperation("分页得到车位信息")
    @GetMapping("/allByPage")
    public AjaxResult getAllByPage(PageQueryReq req){
        List<GarageStall> allByPage = garageStallService.getAllByPage(req);
        // PageInfo 其实就是一个分页的Bean
        PageInfo pageInfo = new PageInfo(allByPage);

        return ResponseTool.success(pageInfo);
    }


    @ApiOperation("修改车位信息")
    @PostMapping("/update")
    public AjaxResult update(GarageStall garageStall) throws HotelException {
        int rel = garageStallService.update(garageStall);
        AssertUtils.assertTrue(rel > 0, ExceptionEnum.STALL_UPDATE_FAIED);

        return ResponseTool.success(PropInfoConstant.UPDATE_SUCCESS);
    }

    @ApiOperation("删除车位信息")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Integer id) throws HotelException {
        int rel = garageStallService.delete(id);
        AssertUtils.assertTrue(rel > 0, ExceptionEnum.STALL_DELETE_FAIED);

        return ResponseTool.success(PropInfoConstant.DELETE_SUCCESS);
    }

}
